<template>
  <a-layout>
    <a-row>
      <a-col span="3">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="inline"
          :open-keys="openKeys"
          :style="{ height: '100%', borderRight: 0 }"
          @click="handleClick"
      >
        <a-menu-item key="welcome">
          <smile-outlined />
          <span>欢迎</span>
        </a-menu-item>
        <a-sub-menu v-for="item in level1" :key="item.id">
          <template #icon>
            <book-outlined/>
          </template>
          <template #title>
            <span>{{item.name}}</span>
          </template>
          <a-menu-item v-for="child in item.children" :key="child.id">
            <span>{{child.name}}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
      </a-col>
      <a-col span="21">
        <a-layout style="padding: 0 24px 24px">
          <a-breadcrumb style="margin: 16px 0">
            <a-breadcrumb-item>Home</a-breadcrumb-item>
            <a-breadcrumb-item>List</a-breadcrumb-item>
            <a-breadcrumb-item>App</a-breadcrumb-item>
          </a-breadcrumb>
          <a-layout-content
              :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
          >
            <div v-show="isShowWelcome">
              <the-welcome></the-welcome>
            </div>

            <a-list v-show="!isShowWelcome" :grid="{ gutter: 16, column: 3 }" item-layout="horizontal" size="large" :pagination="pagination" :data-source="ebooks">
              <template #renderItem="{ item }">
                <a-list-item key="item.name">
                  <template #actions>
<!--                    <span v-for="{ type, text } in actions" :key="type">-->
<!--                      <component :is="type" style="margin-right: 8px" />-->
<!--                      {{ text }}-->
<!--                    </span>-->
                    <span>
                      <component :is="'FileOutlined'" style="margin-right: 8px"/>
                      {{item.docCount}}
                    </span>
                    <span>
                      <component :is="'UserOutlined'" style="margin-right: 8px"/>
                      {{item.viewCount}}
                    </span>
                    <span>
                      <component :is="'LikeOutlined'" style="margin-right: 8px"/>
                      {{item.voteCount}}
                    </span>

                  </template>
                  <a-list-item-meta :description="item.description">
                    <template #title>
                      <router-link :to="'/doc?ebookId='+item.id">{{ item.name }}</router-link>
                    </template>
                    <template  #avatar><a-avatar shape="square" size="large" :src="item.cover" /></template>
                  </a-list-item-meta>
                  {{ item.content }}
                </a-list-item>
              </template>
            </a-list>

          </a-layout-content>
        </a-layout>
      </a-col>
    </a-row>


  </a-layout>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref, reactive, toRef, toRefs} from 'vue';
import axios from "axios";
import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons-vue';
import {Tool} from "@/util/tool";
import {message} from "ant-design-vue";
import TheWelcome from "@/components/the-welcome.vue";

// const listData: Record<string, string>[] = []; //或者直接: any

export default defineComponent({
  name: 'Home',
  components: {
    TheWelcome,
    StarOutlined,
    LikeOutlined,
    MessageOutlined,
  },
  setup(){
    const ebooks = ref();
    const ebooks1 = reactive({books: []});
    const pagination = ref({
      onChange: (page: number) => {
        console.log(page);
      },
      pageSizeOptions: [3, 6, 9],
      defaultPageSize: 3,
      showSizeChanger: true
    });
    const actions: Record<string, string>[] = [
      { type: 'StarOutlined', text: '156' },
      { type: 'LikeOutlined', text: '156' },
      { type: 'MessageOutlined', text: '2' },
    ];
    /**
     * 查询所有分类
     **/
    let categorys: any;
    const level1 = ref();
    const handleQueryCategory = () => {
      axios.get("/category/all").then((response) => {
        const data = response.data;
        if (data.success) {
          // message.success(data.message);
          categorys = data.content;
          console.log("原始数组：", categorys);
          level1.value = [];
          level1.value = Tool.array2Tree(categorys, 0);
          console.log("树形数组：", level1);
        } else {
          message.error(data.message);
        }
      });
    };

    const  isShowWelcome = ref(true);
    const handleClick = (value: any) => {
      console.log("menu click"+JSON.stringify(value.key) +"  "+ value.keyPath );
      if(value.key === 'welcome'){
        isShowWelcome.value = true;
      }else {
        isShowWelcome.value = false;
        let path = value.keyPath.toString();
        let categorys:string[] =  path.split(',');
        axios.get('/ebook/selectByCategory',{
          params: {
          category1Id: categorys[0],
          category2Id: categorys[1]
        }}).then(function (response){
            console.log("res");
            const data = response.data;
            ebooks.value = data.content;
            ebooks1.books = data.content;
        });
      }
    }
    // const handleQueryEbook = () => {
    //   axios.get("/ebook/list", {
    //     params: {
    //       page: 1,
    //       size: 200
    //     }}
    //   ).then((respose) => {
    //     const data = respose.data;
    //     ebooks.value = data.content.list;
    //     ebooks1.books = data.content;
    //   });
    // }
    onMounted(() => {
      handleQueryCategory();
      //axios.get(process.env.VUE_APP_SERVER+"/ebook/list").then((respose) => {
      //main.ts中使用baseUrl之后

      // handleQueryEbook();
    });

    return {
      ebooks,
      ebooks2: toRefs({ebooks1: "books"}),// toRef(ebooks1, "books")
      pagination,
      actions,
      handleQueryCategory,
      level1,
      handleClick,
      isShowWelcome,
    }
  }
});
</script>
